from enum import Enum, unique
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, JSON, BigInteger, TIMESTAMP, text, VARCHAR, CHAR, \
    TEXT
from sqlalchemy.dialects.mysql import LONGTEXT
from sqlalchemy.orm import relationship
from datetime import datetime
import sqlalchemy as sa

from app.models.base import Base, TimestampMixin, BaseEnum, DictData, BaseEnumYesNo


@unique
class AIAgentSyncFlag(BaseEnum):
    ENABLED = DictData('enabled', '启用同步')          # 初始创建状态
    DISABLED = DictData('disabled', '关闭同步') # 生成向量阶段

class AIModelType(BaseEnum):
    OPENAI = DictData('openai', 'openai')
    OPENAI_STREAM = DictData('openai_stream', 'openai流式')#有些模型只支持流式返回
    LLAMA_INDEX = DictData('llama_index', 'llama_index')



class AIConfig(Base, TimestampMixin):
    __tablename__ = "ai_config"
    __comment__ = "AI配置"

    id = Column(Integer, primary_key=True, index=True)
    config_key = Column(String(255), comment="配置key", default='', server_default=text("''"), nullable=False)
    config_value= Column(String(255), comment="配置值", default='', server_default=text("''"), nullable=False)
    config_name= Column(String(255), comment="配置名称", default='', server_default=text("''"), nullable=False)
    description = Column(String(255), comment="描述", default='', nullable=False)
    remark = Column(String(255), comment="备注", default='', nullable=False)
    is_system = Column(String(16),comment="系统默认", default="NO",server_default=text("'NO'"), nullable=False)

    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)


class AIApiKey(Base, TimestampMixin):
    """相当于模型服务"""
    __tablename__ = "ai_api"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(255),comment= "名称[根据该名称关联]", nullable=False)
    type = Column(String(16),comment= "类型",default="openai",server_default=text("'openai'"), nullable=False)
    description = Column(String(255), comment="描述", default='',nullable=False)
    remark = Column(String(255),comment="备注",default='', nullable=True)
    api_base = Column(String(255),comment="接口地址",default='', nullable=True)
    api_key = Column(String(255),comment="接口密钥",default='', nullable=True)
    api_token = Column(String(255),comment="接口令牌",default='', nullable=True)

    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    status = Column(String(16),comment="状态", default="enabled",server_default=text("'enabled'"), nullable=False)
    is_system = Column(String(16),comment="系统默认", default="NO",server_default=text("'NO'"), nullable=False)

    # Relationships
    user = relationship("User", back_populates="ai_api")


class AIModel(Base, TimestampMixin):
    __tablename__ = "ai_model"
    __comment__ = "AI大模型"

    id = Column(Integer, primary_key=True, index=True)
    ai_api_id = Column(Integer, ForeignKey("ai_api.id"), nullable=False)
    model_name = Column(String(255),comment= "模型名称", nullable=False)
    model_group = Column(String(255),comment= "模型分组", nullable=False)
    description = Column(String(255), comment="描述", default='',nullable=False)
    type = Column(String(16),comment="类型", default=AIModelType.LLAMA_INDEX.dictValue,server_default=text(f"'{AIModelType.LLAMA_INDEX.dictValue}'"), nullable=False)
    has_tool= Column(String(16),comment="是否支持tool", default=BaseEnumYesNo.YES.dictValue,server_default=text(f"'{BaseEnumYesNo.YES.dictValue}'"), nullable=False)
    status = Column(String(16),comment="状态", default="enabled",server_default=text("'enabled'"), nullable=False)

class AIAgent(Base, TimestampMixin):
    __tablename__ = "ai_agents"
    __comment__ = "AI助手"

    id = Column(Integer, primary_key=True, index=True,autoincrement=True)
    name = Column(String(255), nullable=False)
    description = Column(String(2048), default='', server_default=text("''"), nullable=False)
    emoji = Column(String(16), default='表情符号', server_default=text("''"), nullable=False)
    group = Column(String(128), default='分组', server_default=text("''"), nullable=False)
    system_prompt = Column(TEXT(16777216), comment='系统提示词',  nullable=True)
    summarize_prompt = Column(String(2048), comment="总结提示词", default='', server_default=text("''"), nullable=False)
    #当智能体作为工具时,该字段为工具说明,为空时,使用 name 字段作为工具说明
    tool_desc = Column(String(2048), comment="作为工具的说明", default='', server_default=text("''"), nullable=False)

    tools = Column(String(2048), comment="工具列表", default='', server_default=text("''"), nullable=False)
    kb_id = Column(Integer, ForeignKey("knowledge_bases.id"), nullable=True)
    agent_ids = Column(String(2048), comment="关联的AI助手ID", default='', server_default=text("''"), nullable=False)
    status = Column(String(16),comment="状态", default="enabled",server_default=text("'enabled'"), nullable=False)
    #同步标识,默认 enabled: 初始化字典时将,删除字典数据,和字典类型后重建,disabled:不删除,在前端修改字典信息后,该值变为0
    sync_flag = Column(String(16), comment="同步标识", default='enabled', server_default=text("'enabled'"), nullable=False)
    ai_model_id = Column(Integer, ForeignKey("ai_model.id"), nullable=True)

    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)

class AIAgentGroup(Base, TimestampMixin):
    __tablename__ = "ai_agents-group"
    __comment__ = "AI助手分组"

    id = Column(Integer, primary_key=True, index=True,autoincrement=True)
    group = Column(String(128), default='分组',unique=True, server_default=text("''"), nullable=False)

    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)

class Tools(Base, TimestampMixin):
    __tablename__ = "tools"
    __comment__ = "工具"

    id = Column(Integer, primary_key=True, index=True)
    tool_name = Column(String(64), default='', server_default=text("''"),nullable=False) #工具名称,唯一
    tool_desc = Column(TEXT(16777216), default=None,nullable=True)
    remark    = Column(String(255), default='', server_default=text("''"),nullable=False)
    status = Column(String(16),comment="状态", default="enabled",server_default=text("'enabled'"), nullable=False)

